package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.questionlib.QuestionUploadActivity;
import com.everflourish.yeah100.entity.question.QuestionUploadNo;
import com.everflourish.yeah100.utils.MyToast;

import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/25.
 * 时间：2016/7/25
 */
public class QuestionUploadItemAdapter extends RecyclerView.Adapter<QuestionUploadItemAdapter.QuestionUploadItemHolder> {

    private List<QuestionUploadNo> mList;
    private Context mContext;
    private LayoutInflater mInflater;
    private String questionType;
    public static int beginNoPosition;
    public static int endNoPosition;

    public QuestionUploadItemAdapter(Context mContext, List<QuestionUploadNo> mList, String questionType) {
        this.mContext = mContext;
        this.mList = mList;
        this.questionType = questionType;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public QuestionUploadItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_question_topic_item, parent, false);
        QuestionUploadItemHolder viewHolder = new QuestionUploadItemHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(QuestionUploadItemHolder holder, final int position) {
        QuestionUploadNo questionUploadNo = mList.get(position);
        holder.questionNo.setText(questionUploadNo.getQuestionNo());
        int isUpload = questionUploadNo.getIsUpload();
        if (isUpload == 0) {// 未上传
            holder.questionItem.setBackgroundColor(mContext.getResources()
                    .getColor(R.color.e_standard_item_null));
        } else {
            holder.questionItem.setBackgroundColor(mContext.getResources()
                    .getColor(R.color.e_standard_item_double));
        }
        holder.questionItem.setOnClickListener(new MyOnclickListener(holder, position, questionUploadNo));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class QuestionUploadItemHolder extends RecyclerView.ViewHolder {

        public LinearLayout questionItem;
        public TextView questionNo;

        public QuestionUploadItemHolder(View itemView) {
            super(itemView);
            questionNo = (TextView) itemView.findViewById(R.id.item_question_topic_no);
            questionItem = (LinearLayout) itemView.findViewById(R.id.item_question_topic_ll);
        }
    }

    class MyOnclickListener implements View.OnClickListener {

        private QuestionUploadItemHolder viewHolder;
        private int position;
        private QuestionUploadNo questionUploadNo;

        public MyOnclickListener(QuestionUploadItemHolder viewHolder, int position, QuestionUploadNo questionUploadNo) {
            this.viewHolder = viewHolder;
            this.position = position;
            this.questionUploadNo = questionUploadNo;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.item_question_topic_ll:
                    if (questionUploadNo.getIsUpload() == 0) {  // 未上传
                        if (QuestionUploadActivity.TOPIC_NO_BEGIN.equals("")) {
                            QuestionUploadActivity.TOPIC_NO_BEGIN = questionUploadNo.getQuestionNo();
                            beginNoPosition = position;
                            viewHolder.questionItem.setBackgroundColor(mContext.getResources()
                                    .getColor(R.color.statistics_indicator));
                            QuestionUploadActivity.TOPIC_TYPE = questionType;
                        } else if (QuestionUploadActivity.TOPIC_NO_END.equals("")) {
                            if (QuestionUploadActivity.TOPIC_NO_BEGIN.equals(questionUploadNo.getQuestionNo())) {
                                viewHolder.questionItem.setBackgroundColor(mContext.getResources()
                                        .getColor(R.color.e_standard_item_null));
                                QuestionUploadActivity.TOPIC_NO_BEGIN = "";
                                return;
                            }
                            endNoPosition = position;
                            if (isContainUploaded()) {
                                QuestionUploadActivity.TOPIC_NO_BEGIN = "";
                                QuestionUploadActivity.TOPIC_NO_END = "";
                                QuestionUploadActivity.TOPIC_TYPE = "";
                                beginNoPosition = 0;
                                endNoPosition = 0;
                                notifyDataSetChanged();
                                MyToast.showShort(mContext, "选中的题号中包含了已上传题号");
                                return;
                            }
                            QuestionUploadActivity.TOPIC_NO_END = questionUploadNo.getQuestionNo();
                            viewHolder.questionItem.setBackgroundColor(mContext.getResources()
                                    .getColor(R.color.statistics_indicator));
                        } else {
                            QuestionUploadActivity.TOPIC_NO_BEGIN = "";
                            QuestionUploadActivity.TOPIC_NO_END = "";
                            QuestionUploadActivity.TOPIC_TYPE = "";
                            beginNoPosition = 0;
                            endNoPosition = 0;
                            notifyDataSetChanged();
                        }
                    } else {  // 已上传
                        MyToast.showShort(mContext, "该道题目已经上传");
                        return;
                    }
                    break;
            }
        }
    }

    /**
     * 判断是否包含已上传题号
     *
     * @return
     */
    private boolean isContainUploaded() {
        if (beginNoPosition > endNoPosition) {
            int temp = beginNoPosition;
            beginNoPosition = endNoPosition;
            endNoPosition = temp;
        }
        for (int i = beginNoPosition; i <= endNoPosition; i++) {
            if (mList.get(i).getIsUpload() == 0) {
                continue;
            } else {
                return true;
            }
        }
        return false;
    }
}